package com.itheima.health.service.impl;



import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;

import com.itheima.health.service.OrderSettingService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


import java.util.Date;


/**
 * @author ：靳鸿鸿鸿曦
 * @date ：Created in 2022年06月09日
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService{
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public List<OrderSetting> getOrderSettingByMonth(int year, int month) {


        //构造日期范围
        String startDate = String.format("%d-%d-1", year, month);
        String endDate = String.format("%d-%d-31", year, month);
        //DAO查询并返回
        return orderSettingDao.selectOrderSettingByMonth(startDate, endDate);
    }


    @Override
    public void editNumberByDate(Date orderDate, int number) {
        log.info("[根据日期,设置预约信息]date",orderDate,number);

       OrderSetting os =  orderSettingDao.selectByOrderDate(orderDate);

        if (null!= os){
            orderSettingDao.updateNumberByDate(os.getId(),number);
        }else {
            orderSettingDao.add(new OrderSetting(orderDate,number));
        }
    }


    @Override
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings!=null&& orderSettings.size()>0){
            for (final OrderSetting orderSetting : orderSettings) {
                OrderSetting select = orderSettingDao.selectByOrderDate(orderSetting.getOrderDate());
                if (select!=null){
                    orderSettingDao.updateNumberByDate(select.getId(),orderSetting.getNumber());
                }
                if (select==null){
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

}